package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.Scroller;

/**
 * Created by jiayen on 17/3/31.
 */

public class PullDownLayout  extends RelativeLayout {

    private GestureDetector  gestureDetector;
    private Context  mContext;

    private Scroller scroller;
    public PullDownLayout(Context context) {
        this(context,null);
    }

    public PullDownLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PullDownLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        scroller = new Scroller(mContext);
    }


    private int startx;
    private boolean isMoved;
    private int padding;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                    startx = (int) event.getX();
                break;
            case  MotionEvent.ACTION_UP:
                padding+=(event.getX()-startx);
                setPadding(0,padding,0,0);
                isMoved=true;
                break;
            case MotionEvent.ACTION_MOVE:
                if(isMoved){
                    int endX = (int) event.getX();
                   scrollTo(endX-startx,0);
                    isMoved=false;
                }
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

}
